package com.car.action.api;

import java.util.List;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardLike;
import com.car.common.model.MessageUnreadNum;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.utils.APIJSON;
import com.utils.StringUtil;

public class MessageController extends CommonController {
	
	
    /** 
     * 评论我的
     */
    public void discuss_me(){
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
	    	MessageUnreadNum.dao.clearNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_DISCUSS, user_id.intValue());
	    	int page = 1;
	        if(!StringUtil.empty(getPara("page"))) {
	        	page = getParaToInt("page");
	        }
	    	List<Map<String, Object>> result = CardDiscuss.dao.sel_my_list(user_id.intValue(), page);
	    	renderJson(APIJSON.sendJSON(result));
		}
    }
    
    
    /**
     * 点赞我的
     */
    public void like_me(){
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
	    	MessageUnreadNum.dao.clearNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_LIKE, user_id.intValue());
	    	int page = 1;
	        if(!StringUtil.empty(getPara("page"))) {
	        	page = getParaToInt("page");
	        }
	    	List<Map<String, Object>> result = CardLike.dao.sel_my_list(user_id.intValue(), page );
	    	renderJson(APIJSON.sendJSON(result));
		}
    }
    
    public void num() {
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			MessageUnreadNum one = MessageUnreadNum.dao.findFirst("select CAST(SUM(IF(type=1,num,0)) AS UNSIGNED) friend_num,CAST(SUM(IF(type=2,num,0)) AS UNSIGNED) discuss_num,CAST(SUM(IF(type=3,num,0)) AS UNSIGNED) remind_num,CAST(SUM(IF(type=4,num,0)) AS UNSIGNED) like_num,CAST(SUM(IF(type=11,num,0)) AS UNSIGNED) express_num,CAST(SUM(IF(type in (1,2,3,4,11),num,0)) AS UNSIGNED) all_num from message_unread_num where user_id = " + user_id);
			
			Map<String, Object> result = Maps.newHashMap();
			result.put("friend_num",  	one == null ? 0 : one.get("friend_num") == null ? 0 : one.get("friend_num"));
			result.put("discuss_num",  	one == null ? 0 : one.get("discuss_num") == null ? 0 : one.get("discuss_num"));
			result.put("remind_num",  	one == null ? 0 : one.get("remind_num") == null ? 0 : one.get("remind_num"));
			result.put("like_num",  	one == null ? 0 : one.get("like_num") == null ? 0 : one.get("like_num"));
			result.put("express_num",  	one == null ? 0 : one.get("express_num") == null ? 0 : one.get("express_num"));
			result.put("all_num",  	one == null ? 0 : one.get("all_num") == null ? 0 : one.get("all_num"));
			
			
			renderJson(APIJSON.sendJSON(result));
		}
    }
    
    
    public void home_num() {
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			MessageUnreadNum one = MessageUnreadNum.dao.findFirst("select CAST(SUM(IF(type=1,num,0)) AS UNSIGNED) friend_num,CAST(SUM(IF(type=5,num,0)) AS UNSIGNED) fans_num,CAST(SUM(IF(type=6,num,0)) AS UNSIGNED) see_num,CAST(SUM(IF(type=7,num,0)) AS UNSIGNED) order_num,CAST(SUM(IF(type=11,num,0)) AS UNSIGNED) express_num ,CAST(SUM(IF(type=8,num,0)) AS UNSIGNED) gift_num from message_unread_num where user_id = " + user_id);
			
			Map<String, Object> result = Maps.newHashMap();
			if(one != null) {
				result.put("friend_num",  	one.get("friend_num") == null ? 0 : one.get("friend_num"));
				result.put("fans_num",  	one.get("fans_num") == null ? 0 : one.get("fans_num"));
				result.put("see_num",   	one.get("see_num") == null ? 0 : one.get("see_num"));
				result.put("order_num", 	one.get("order_num") == null ? 0 : one.get("order_num"));
				result.put("express_num",   one.get("express_num") == null ? 0 : one.get("express_num"));
				result.put("gift_num",  	one.get("gift_num") == null ? 0 : one.get("gift_num"));
				result.put("task_num",   	0);
			}
			renderJson(APIJSON.sendJSON(result));
		}
    }
}
